<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


abstract class sfThBase extends sfTableWidget{
    
    protected $widget = null;
    protected $validator = null;

    /**
     * Constructor.
     *
     * Available options:
     *
     *  * id_format:       The format for the generated HTML id attributes (%s by default)
     *  * is_hidden:       true if the form widget must be hidden, false otherwise (false by default)
     *  * default:         The default value to use when rendering the widget
     *  * title:           The title to use when the widget is rendered
     *  * link:            The link of the widget
     * 
     *  * text:             The text to render in the cell
     *  * filtrable:        Boolean 
     *  * filterType:       Filter type
     *  * fixedConditions:  Conditions to the query
     *  * filterLabel:      Label of the input filter
     *  * filterItems:      List of items in multi options inputs
     *  * filterPosition:   Position in filter
     *  * filterCol:        Column in filter
     * 
     * @param array $options     An array of options
     * @param array $attributes  An array of default HTML attributes
     *
     * @see sfWidget
     */
    
    public function __construct($options = array(), $attributes = array())
    {
        $this->addOption('text', null);     
        $this->addOption('fixedConditions', array());     
        
        $this->addOption('filterable', false);           
        $this->addOption('filterType', '');     
        $this->addOption('filterLabel', '');     
        $this->addOption('filterPosition', '');             
        $this->addOption('filterGroup', 'General');                     
        $this->addOption('filterItems', array());     
        $this->addOption('multiText', false);                   
        $this->addOption('customFilter', '');                   
        $this->addOption('filter', false);

        
        $this->addOption('sort', false);     
        $this->addOption('sortable', true);             
        $this->addOption('sortDirection', 'asc');             

        
        
      parent::__construct($options, $attributes);
    }    
    

/**
 * Las condiciones fijas son restricciones al resultado de la query
 * que se aplican siempre. 
 * El formato del string es '[operador] [valor/valores]' 
 * Ejemplos:
 *          'LIKE "san%"' 
 *          '= 1 '
 *          'IN (1,2,3)'
 * @param type $fixedCondition
 */
    public function addFixedCondition($fixedCondition) {
        
          $fixedConditions = $this->getOption('fixedConditions');                
          $fixedConditions[] = $fixedCondition;
          $this->setOption('fixedConditions', $fixedConditions);     
          return $this;
    }
    
    public function getFixedConditions() {
      return $this->getOption('fixedConditions');        
    }
    
    
    public function setSort() {
          $this->setOption('sort', true);
          return $this;                
    }

    
    
    
    public function getSortDirection() {
        return $this->getOption('sortDirection');
    }

    public function setSortDirection($direction) {
        if(($direction == 'asc') || ($direction == 'desc'))
        {
          $this->setOption('sortDirection', $direction);
        }
        return $this;                    
    }

    public function isSortable() {
      return $this->getOption('sortable');
    }

    public function isSort() {
      return $this->getOption('sort');
    }

    public function setSortable($sortable = true) {
          $this->setOption('sortable', $sortable);
          return $this;                
    }

    public function getFilter() {
      return $this->getOption('filter');        
    }

    public function setFilter($filter = true) {
          $this->setOption('filter', $filter);
          return $this;
    }
    
    public function isFilterable() {
      return $this->getOption('filterable');        
    }
    
    public function setFilterable($filtrable = true, $filterLabel ='') {
          $this->setOption('filterable', $filtrable);     
          $this->setOption('filterLabel', $filterLabel);
          return $this;
    }

    public function getFilterType() {
      return $this->getOption('filterType');        
    }

    public function setFilterType($filterType) {
          $this->setOption('filterType', $filterType);
          return $this;
    }
    
    public function getFilterPosition() {
      return $this->getOption('filterPosition');        
    }

    public function setFilterPosition($filterPosition) {
          $this->setOption('filterPosition', $filterPosition);
          return $this;
    }

    public function getFilterCol() {
      return $this->getOption('filterCol');        
    }

    public function setFilterCol($filterCol) {
          $this->setOption('filterCol', $filterCol);
          return $this;
    }
    
    public function setFilterItems($filterItems) {
          $this->setOption('filterItems', $filterItems);
          return $this;
    }

    public function getFilterItems() {
      return $this->getOption('filterItems');
    }
        
    public function getFilterLabel() {
      return $this->getOption('filterLabel');        
    }
    
    
    public function setWidget($widget) {
        $this->widget = $widget;
    }

    public function getWidget() {
        return $this->widget;
    }
    
    public function setValidator($validator) {
        
        $this->validator = $validator;
    }

    public function getValidator() {
        return $this->validator;
    }

    
    public function setText($text) {
          $this->setOption('text', $text);
          return $this;        
    }

    public function getText() {
      return $this->getOption('text');
    }
    
    
    public function isHidden() {
       return $this->getOption('is_hidden');
    }
    
    public function setHidden($hidden = true) {
          $this->setOption('is_hidden', $hidden);
          return $this;                
    }
    
    public function render($content = '') {
        if($content=='')$content=$this->getDefault();
        if(($link = $this->getOption('link')) != '')
            $result = $this->renderContentTag('th', 
                            $this->renderContentTag('a',
                                    strtr($this->getOption('template'),array('%content%'=>$content))
                                    ,array('href'=>$link)), $this->getAttributes());        
        else
            $result = $this->renderContentTag('th',
                                    strtr($this->getOption('template'),array('%content%'=>$content))
                                    , $this->getAttributes());
       
        return $result;
    }
    
    
}


?>
